package com.yuhong.reggie.service.impl;

import com.yuhong.reggie.entity.*;
import com.yuhong.reggie.mapper.*;
import com.yuhong.reggie.service.OrderService;
import com.yuhong.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderSericeImpl implements OrderService {
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;



    @Override
    public void addSvae(Orders orders, Long userId) {

        //根据传递过来的默认的收获地址id获取地址对象AddressBook
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        //根据传递过来的登录用户id获取购物车数据list<ShoppingCart>
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.findAllByUserId(userId);
        //根据登录用户的id获取登录用户对象(得到用户别名写入到订单数据)
        User user = userMapper.findById(userId);

        //完善订单对象Orders数据
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        orders.setId(oid);
        orders.setNumber(oid+"");
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        //商品总价
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());

        //完善订单明细对象List<OrderDetail>
        List<OrderDetail> orderDetailList = new ArrayList<>();
        BigDecimal total = new BigDecimal(0);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail1 = new OrderDetail();
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail1.setId(orderDetailId);
            orderDetail1.setNumber(shoppingCart.getNumber());
            orderDetail1.setImage(shoppingCart.getImage());
            orderDetail1.setOrderId(orders.getId());
            orderDetail1.setDishId(shoppingCart.getDishId());
            orderDetail1.setSetmealId(shoppingCart.getSetmealId());
            orderDetail1.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail1.setNumber(shoppingCart.getNumber());
            orderDetail1.setAmount(shoppingCart.getAmount());

            orderDetailList.add(orderDetail1);
            //总金额
            total.add(orderDetail1.getAmount().multiply(new BigDecimal(orderDetail1.getNumber())));

            //调用mapper新增订单主表数据
            ordersMapper.save(orders);
            //订单详情
            orderDetailMapper.saveBatch(orderDetailList);
            //清空购物车
            shoppingCartMapper.delete(userId);
        }
    }
}
